const { ccclass, property } = cc._decorator;

@ccclass
export default class MyAnysdkMgr extends cc.Component {

    sdkManager: anysdk.AgentManager = null;

    /**登录后台验证地址 */
    oauthLoginServer = '192.168.188.35:9001/sdkLogin';

    /**anysdk的appkey */
    appKey = '010E988C-E8D4-7299-F5D1-9CDC7737A256';

    /**anysdk的秘钥 */
    appSecret = '24e9aa84c78490b535143182e3e8b671';
    /**anysdk的公钥 */
    privateKey = '0DA37730F986B117B76AD729AD730EB9';

    userPlugin: anysdk.ProtocolUser = null;

    /**初始化插件 */
    onLoad() {
        if (cc.sys.isNative) {
            cc.vv.anysdk = this;
            console.log('00000000000');
            console.log(anysdk.agentManager);
            console.log('00000000000');
            this.sdkManager = anysdk.agentManager;
            this.sdkManager.init(this.appKey, this.appSecret, this.privateKey, this.oauthLoginServer);
            this.userPlugin = this.sdkManager.getUserPlugin();
            console.log('获取插件');
            if (this.userPlugin) {
                this.userPlugin.setListener(this.onUserResult, MyAnysdkMgr);
                console.log('设置插件监听');
            }
        }
    }


    /**用户登录 */
    login() {
        // console.log('进入登录函数');
        // console.log('userPlugin为：', this.userPlugin);
        // if (this.userPlugin) {
        //     console.log('111111111111');
        //     // this.userPlugin.setLoginType('wx');
        //     console.log('222222222222');
        //     this.userPlugin.login();
        //     console.log('开始登录');
        // }
        const ret = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/Test', 'hello', '(Ljava/lang/String;)Ljava/lang/String;', '是的罚款自己和');
        console.log(ret);
    }


    /**监听用户登录 */
    onUserResult(code, msg) {
        switch (code) {
            // 用户系统初始化成功
            case anysdk.UserActionResultCode.kInitSuccess:
                console.log('初始化插件成功', msg);
                break;
            // 用户系统初始化失败
            case anysdk.UserActionResultCode.kInitFail:
                console.log('初始化插件失败', msg);
                break;
            // 用户系统网络错误
            case anysdk.UserActionResultCode.kLoginNetworkError:
                break;
            // 用户系统无需登录
            case anysdk.UserActionResultCode.kLoginNoNeed:
                break;
            // 用户系统登录失败
            case anysdk.UserActionResultCode.kLoginFail:
                break;
            // 用户系统登录取消
            case anysdk.UserActionResultCode.kLoginCancel:
                break;
            // 用户系统登录取消
            case anysdk.UserActionResultCode.kLoginSuccess:
                console.log('用户登录成功,用户信息', this.userPlugin.getUserInfo);
                break;

        }
    }
}


